<template>
  <section>
    <!--工具条-->
    <ue-search-bar v-model="searchFilters" :data="querys">
    </ue-search-bar>

    <!-- 操作 -->
    <el-col :span="24" style="margin-bottom: 10px">
      <el-button type="primary" @click="handleAdd">新增</el-button>
      <el-button type="primary" @click="handleAdd1">新增1</el-button>
      <el-button type="primary" @click="getTableData">刷新</el-button>
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-button type="primary" @click="resetCondition" style="float: right">重置</el-button>
      <el-button type="primary" v-on:click="getTableData" style="float: right">查询</el-button>
    </el-col>

    <!--列表-->
    <el-table :data="tableData" highlight-current-row border @sort-change="sortChange"
              @selection-change="selsChange" v-loading="listLoading" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="email" label="邮件" >
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" width="130">
      </el-table-column>
      <el-table-column prop="birthday" label="生日" width="180">
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button  @click="handleEdit1(scope.$index, scope.row)">编辑1</el-button>
          <el-button type="danger"  @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next, jumper"
        :total="total" style="float: right">
      </el-pagination>
    </el-col>

    <!-- 弹窗 -->
    <el-dialog :title="dialogTitle" size="tiny" :visible.sync="myFormVisible" :close-on-click-modal="false">
      <el-form :model="myForm" :rules="rules" label-width="80px" ref="myForm">
        <el-form-item label="别名" prop="alias">
          <el-input v-model="myForm.alias" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="myForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="myForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮件" prop="email">
          <el-input v-model="myForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="myForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker type="date" placeholder="选择日期" v-model="myForm.birthday"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="myFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="handleSubmit" :loading="submitLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import { savePartners, batchPartners } from '../../api/system/partner';
export default {
  data () {
    return {
      searchFilters: {},
      dialogTitle: '新增',
      condition: {
        name: '',
        number: ''
      },
      sels: [],
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      sortProp: null,
      sortOrder: 'ascending',
      listLoading: false,
      myFormVisible: false,
      submitLoading: false,
      // 表单数据
      myForm: {
        'id': null,
        'password': null,
        'alias': null,
        'email': null,
        'name': null,
        'phone': null,
        'sex': null,
        'status': null,
        'type': null,
        'birthday': null
      },
      // 验证规则
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        alias: [
          {required: true, message: '请输入别名', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮件', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号码', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    // 弹出修改窗口
    handleEdit (index, row) {
      this.dialogTitle = '修改';
      this.resetForm();
      this.myFormVisible = true;
      this.myForm = Object.assign({}, row);
    },
    // 跳转到修改页面
    handleEdit1 (index, row) {
      this.$router.push('/order/table_modify/' + row.id);
    },
    // 删除单条数据操作
    handleDel (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        batchPartners(row.id).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getTableData();
        });
      }).catch(() => {

      });
    },
    // 新增修改数据提交
    handleSubmit () {
      // 表单验证
      this.$refs['myForm'].validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          savePartners('xxx', this.myForm).then((res) => {
            this.submitLoading = false;
            if (res.data.successFlg === true) {
              this.myFormVisible = false;
              this.getTableData();
            } else {
              this.$message.error(res.data.errorMsg);
            }
          }).catch(function (error) {
            console.log(error);
          });
        }
      });
    },
    // 重置表单
    resetForm () {
      if (this.$refs.myForm) {
        this.$refs.myForm.resetFields();
      }
      // 当先点击修改，再点击新增，会出现值没有清空,修复处理
      for (let p in this.myForm) {
        if (this.myForm[p] instanceof Array) {
          this.myForm[p] = [];
        } else {
          this.myForm[p] = null;
        }
      }
    },
    // 弹出添加窗体
    handleAdd () {
      this.dialogTitle = '新增';
      this.resetForm();
      this.myFormVisible = true;
    },
    // 跳转到新增页面
    handleAdd1 () {
      this.$router.push('/order/table_add');
    },
    // 多选操作
    selsChange (sels) {
      this.sels = sels;
    },
    // 排序操作
    sortChange (o) {
      this.sortProp = o.prop;
      this.sortOrder = o.order;
      this.getTableData();
    },
    // 每页几条操作
    handleSizeChange (val) {
      this.pageSize = val;
      this.getTableData();
    },
    // 第几页操作
    handleCurrentChange (val) {
      this.currentPage = val;
      this.getTableData();
    },
    // 重置查询条件
    resetCondition () {
      for (let p in this.condition) {
        this.condition[p] = null;
      }
    },
    // 获取表格数据
    getTableData () {
      console.log(this.searchFilters);
    },
    // 批量删除
    batchRemove () {
      let ids = this.sels.map(item => item.id);
      batchPartners(ids).then((res) => {
        this.getTableData();
      });
    }
  },
  computed: {
    btns () {
      return this.$store.state.btns[this.$route.path] || [];
    },
    querys () {
      return this.$store.state.querys[this.$route.path + '#default'] || [];
    }
  },
  // 初始化数据
  mounted () {
    console.log(this.querys);
    this.$store.commit('initDict');
    this.$store.commit('initOrganizations');
    // this.getTableData();
  }
};
</script>

<style lang="less">

</style>
